package com.ruoyi.channel.service.impl;

import com.ruoyi.channel.domain.ChannelWallet;
import com.ruoyi.channel.domain.ChannelWalletRSP;
import com.ruoyi.channel.domain.MpChannel;
import com.ruoyi.channel.mapper.ChannelWalletMapper;
import com.ruoyi.channel.mapper.MpChannelMapper;
import com.ruoyi.channel.service.IChannelWalletService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.text.Convert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *  服务层实现
 * 
 * @author ruoyi
 * @date 2019-05-08
 */
@Service
public class ChannelWalletServiceImpl implements IChannelWalletService
{
	Logger logger = LoggerFactory.getLogger(ChannelWalletServiceImpl.class);

	/** 渠道钱包添加返回结果 */
	public final static int CHANNELWALLET_ADD_SUCCESS = 1;
	public final static int CHANNELWALLET_ADD_FALSE = -1;

	@Autowired
	private ChannelWalletMapper channelWalletMapper;
	@Autowired
	private MpChannelMapper mpChannelMapper;

	/**
     * 查询信息
     * 
     * @param id ID
     * @return 信息
     */
    @Override
	public ChannelWallet selectChannelWalletById(Long id)
	{
	    return channelWalletMapper.selectChannelWalletById(id);
	}
	
	/**
     * 查询列表
     * 
     * @param channelWallet 信息
     * @return 集合
     */
	@Override
	public List<ChannelWallet> selectChannelWalletList(ChannelWallet channelWallet)
	{
	    return channelWalletMapper.selectChannelWalletList(channelWallet);
	}
	
    /**
     * 新增
     * 
     * @param channelWallet 信息
     * @return 结果
     */
	@Override
	public int insertChannelWallet(ChannelWallet channelWallet) {
		return channelWalletMapper.insertChannelWallet(channelWallet);
	}
	
	/**
     * 修改
     * 
     * @param channelWallet 信息
     * @return 结果
     */
	@Override
	public int updateChannelWallet(ChannelWallet channelWallet)
	{
	    return channelWalletMapper.updateChannelWallet(channelWallet);
	}

	/**
     * 删除对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteChannelWalletByIds(String ids)
	{
		return channelWalletMapper.deleteChannelWalletByIds(Convert.toStrArray(ids));
	}


	/**
	 * 根据搜索框的条件进行查询
	 *
	 * @param channelIdList 渠道类型id集合
	 * @return 渠道钱包集合
	 */
	@Override
	public List<ChannelWallet> seachChannelWalletList(List<Long> channelIdList) {
		return channelWalletMapper.seachChannelWalletList(channelIdList);
	}


	/**
	 * 添加渠道时，给每个渠道赋值
	 * @param channelList 		渠道集合
	 * @param channelWallet		渠道钱包（前台数据）
	 * @return					渠道钱包集合
	 */
	public List<ChannelWallet> makeChannelWalletListToAdd (List<MpChannel> channelList, ChannelWallet channelWallet) {
		// 要添加的渠道钱包集合
		List<ChannelWallet> addChannelWalletList = new ArrayList<>();
		for (MpChannel addMpChannel : channelList) {
			// 每个渠道钱包赋值
			ChannelWallet addChannelWallet = new ChannelWallet();
			// 渠道标识
			addChannelWallet.setChannelId(addMpChannel.getId());
			// 余额
			addChannelWallet.setAccountBalance(channelWallet.getAccountBalance());
			// 待结算金额
			addChannelWallet.setWaitLiquidatedBalance(channelWallet.getWaitLiquidatedBalance());

			// 假如渠道钱包集合
			addChannelWalletList.add(addChannelWallet);
		}
		return addChannelWalletList;
	}


	/**
	 * 根据渠道id和钱包类型id  删除渠道钱包
	 *
	 * @param channelId    渠道id
	 * @param walletKindId 钱包类型id
	 */
	@Override
	public void deleteByChannelIdAndWalletKindId(Long channelId, Long walletKindId) {
		ChannelWallet channelWallet = new ChannelWallet();
		channelWallet.setChannelId(channelId);
		List<ChannelWallet> channelWalletList = channelWalletMapper.selectChannelWalletList(channelWallet);
		if (channelWalletList.size() >= 1) {
			List<String> idList = new ArrayList<>();
			for (int i = 0; i < channelWalletList.size(); i++) {
				idList.add(String.valueOf(channelWalletList.get(i).getId()));
			}
			idList.remove(0);
			String[] ids = new String[idList.size()];
			channelWalletMapper.deleteChannelWalletByIds(idList.toArray(ids));
		}
	}

	/**
	 * 查询列表
	 *
	 * @param channelWallet 信息
	 * @return 集合
	 */
	@Override
	public List<ChannelWalletRSP> selectChannelWalletRSPList(ChannelWallet channelWallet)
	{
		return channelWalletMapper.selectChannelWalletRSPList(channelWallet);
	}



	/**
	 * 根据渠道方法id查询渠道钱包信息
	 *
	 * @param channelMethodId 渠道方法id
	 * @return 渠道钱包信息
	 */
	@Override
	public List<ChannelWallet> selectChannelWalletByChannelMethodId(Long channelMethodId) {
		return channelWalletMapper.selectChannelWalletByChannelMethodId(channelMethodId);
	}
}
